
package com.example.jebaczlodzieji;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class MenuActivity extends Activity {
        
        ExpandableListAdapter listAdapter;
        ExpandableListView expListView;
        List<String> listDataHeader;
        HashMap<String, List<Product>> listDataChild;
        int down = 0;
        
        @Override
        public void onBackPressed(){
                if (down ==0){
                        Context dupa = getApplicationContext();
                        CharSequence kutas = "Naprawdê chcesz wyjæ z aplikacji?";
                        int cycki = Toast.LENGTH_SHORT;
                        down++;
                        Toast wigga = Toast.makeText(dupa, kutas, cycki);
                        wigga.show();
                }else{
                        System.exit(0);
                }
                Log.d("com.example.jebaczlodzieji", "klawisz Back");//log cat gdy urzytkownik klika klawisz back
        }
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_menu);
                
                // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);
 
        // preparing list data
        prepareListData();
 
        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
 
        // setting list adapter
        expListView.setAdapter(listAdapter);
        
        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {

                        @Override
                        public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                                Toast.makeText(getApplicationContext(),
                                                listDataHeader.get(groupPosition) + " : "+ listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition),
                                        Toast.LENGTH_SHORT)
                        .show();
                                int taks=listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).getTask();
                                withTaks(taks, v);
                                return false;
                        }
                
        });
        }
        /*
     * Preparing the list data
     */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<Product>>();
 
        // Adding child data
        listDataHeader.add("Top 250");
        listDataHeader.add("Now Showing");
        listDataHeader.add("Coming Soon..");
 
        // Adding child data
        List<Product> top250 = new ArrayList<Product>();
        top250.add(new Product(1,"The Shawshank Redemption",5));
        top250.add(new Product(2,"The Godfather",8));
        top250.add(new Product(3,"The Godfather: Part II",23));
        top250.add(new Product(4,"Pulp Fiction",5));
        top250.add(new Product(5,"The Good, the Bad and the Ugly",8));
        top250.add(new Product(6,"The Dark Knight",23));
        top250.add(new Product(7,"12 Angry Men",5));
 
        List<Product> nowShowing = new ArrayList<Product>();
        nowShowing.add(new Product(1,"The Conjuring",8));
        nowShowing.add(new Product(2,"Despicable Me 2",5));
        nowShowing.add(new Product(3,"Turbo",23));
        nowShowing.add(new Product(4,"Grown Ups 2",5));
        nowShowing.add(new Product(10,"Red 2",8));
        nowShowing.add(new Product(6,"The Wolverine",23));
 
        List<Product> comingSoon = new ArrayList<Product>();
        comingSoon.add(new Product(1,"2 Guns",8));
        comingSoon.add(new Product(2,"The Smurfs 2",5));
        comingSoon.add(new Product(20,"The Spectacular Now",23));
        comingSoon.add(new Product(4,"The Canyons",23));
        comingSoon.add(new Product(5,"Europa Report",8));
 
        listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);
    }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.menu, menu);
                return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();
                if (id == R.id.action_settings) {
                        return true;
                }
                return super.onOptionsItemSelected(item);
        }
        public void podatek5(View view){
                Intent indu = new Intent(this,PiecProcet.class);
                startActivity(indu);
                finish();
        }
        public void podatek8(View view){
                Intent indu = new Intent(this,OsiemProcent.class);
                startActivity(indu);
                finish();
        }
        public void podatek23(View view){
                Intent indu = new Intent(this,PodatekDwadziesciaTrzy.class);
                startActivity(indu);
                finish();
        }
        public void withTaks(int taks,View view){
                switch (taks) {
                case 5:
                        podatek5(view);
                        break;
                case 8:
                        podatek8(view);
                        break;
                case 23:
                        podatek23(view);
                        break;

                default:
                        break;
                }
                
        }
}